package com.example.cinema;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.cinema.adapter.PostsRecyclerAdapter;
import com.example.cinema.adapter.RecyclerAdapter;
import com.example.cinema.bindingdata.Blank2GridData;
import com.example.cinema.bindingdata.UserCircleData;
import com.example.cinema.bindingdata.UserItemViewData;
import com.example.cinema.databinding.FragmentBlank2Binding;
import com.example.cinema.drawview.CircleVideoPlayer;
import com.example.cinema.request.RequestData;
import com.example.cinema.testdara.TestData;

import java.util.ArrayList;
import java.util.List;

import tv.danmaku.ijk.media.player.IjkMediaPlayer;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link BlankFragment2#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BlankFragment2 extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public BlankFragment2() {
        // Required empty public constructor
    }


    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {

        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment BlankFragment2.
     */
    // TODO: Rename and change types and number of parameters
    public static BlankFragment2 newInstance(String param1, String param2) {
        BlankFragment2 fragment = new BlankFragment2();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }

    private static FragmentBlank2Binding FBB;
    private static boolean isinit;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (!isinit) {
            FBB = DataBindingUtil.inflate(inflater, R.layout.fragment_blank2, container, false);
        }
        if(Build.VERSION.SDK_INT>11 && Build.VERSION.SDK_INT<19)
        {
            View view = container.getRootView();
            view.setSystemUiVisibility(View.GONE);
        }else if(Build.VERSION.SDK_INT>=19){
            View view = container.getRootView();
            int UIOP = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY|View.SYSTEM_UI_FLAG_FULLSCREEN;
            view.setSystemUiVisibility(UIOP);
        }

        return FBB.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        if (!isinit) {
            isinit = true;
            init();
            initdata();
        }

    }

    private UserAttribute attribute;
    private void init() {
        attribute = (UserAttribute) getActivity().getApplication();
        Log.i("LDDD",attribute.getAppMoodMode()+"");
        if(attribute.getAppMoodMode()==1)
        {
            Log.i("LDDD","关闭");
            FBB.send.setVisibility(View.INVISIBLE);
        }else {
            //发帖
            FBB.send.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();

                    if(attribute.getAppMode()!=0)
                    {
                        intent.setAction("addcircle");
                        intent.setAction("sign");
                        startActivity(intent);
                        return;
                    }
                    if(attribute.isVipbool())
                    {
                        intent.setAction("addcircle");
                        startActivity(intent);
                    }else {
                        Toast.makeText(getContext(),"只有VIP才可以发布哦",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }


        //滚动监听
        FBB.scroll.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                int total = v.getChildAt(0).getMeasuredHeight();
                int heith = v.getMeasuredHeight();
                int y = total - heith;
                if (scrollY >= y) {
                    p=p+1;
                    FBB.uptexte.setText("努力加载中!");
                    ThreadDataList();
                    return;
                }
            }
        });
    }

    //初始化数据
    private void initdata() {
        p = 1;
        ThreadDataList();
    }

    //获取数据
    private int p = 1;
    private int l = 30;
    private List<UserItemViewData> dataList = new ArrayList<>();

    private void ThreadDataList() {
        new Thread() {
            @Override
            public void run() {
                RequestData requestData = new RequestData(getContext());
                List<UserItemViewData> data = requestData.GetMoodeData(p, l);
                Log.i("HHHH", data.size() + "数量");
                if(data.size()>0)
                {
                    dataList.addAll(data);
                    handler.sendEmptyMessage(1000);
                }else {
                    handler.sendEmptyMessage(1001);
                }
            }
        }.start();
    }

    //渲染
    Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == 1000) {
                for (int i = 0; i < dataList.size(); i++) {
                    ViewDataBinding view = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.user_circle_item, null, false);
                    view.setVariable(BR.circledata, dataList.get(i));
                    FBB.lineargroup.addView(view.getRoot());
                }
            }
            if (msg.what == 1001)
            {
                FBB.uptexte.setText("没有更多了！");
            }
        }
    };
}